package per.sunmes.lesrb.actor;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;

import per.sunmes.lesrb.apis.CallBackObj;
import per.sunmes.lesrb.manager.SM;
import per.sunmes.lesrb.util.DU;
import per.sunmes.lesrb.util.U;

public class BlendBtn extends Actor {

	TextureRegion trNormal;
	public CallBackObj<BlendBtn> clickCall;
	boolean isTouched;

	float[] overflows = new float[4];

	public static String btnSoundEffectPath;
	float blendAlpha = 0.35f;

	/** 0-3 left,right,top,bottom */
	public float[] getOverflows() {
		return overflows;
	}

	public void setOverflows(float left, float right, float top, float bottom) {
		overflows[0] = left;
		overflows[1] = right;
		overflows[2] = top;
		overflows[3] = bottom;
	}

	public void setOverflowsAndDesSize(float left, float right, float top, float bottom) {
		setOverflows(left, right, top, bottom);
		setSize(getWidth() - left - right, getHeight() - top - bottom);
		moveBy(left, bottom);
	}

	public float getBlendAlpha() {
		return blendAlpha;
	}

	public void setBlendAlpha(float blendAlpha) {
		this.blendAlpha = blendAlpha;
	}

	public BlendBtn(String normal) {
		trNormal = U.region(normal);

		setSize(trNormal.getRegionWidth(), trNormal.getRegionHeight());

		addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				isTouched = true;

				if (btnSoundEffectPath != null) {
					SM.playAssetSound(btnSoundEffectPath);
				}

				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				if (x < 0 || x > getWidth() || y < 0 || y > getHeight()) {

				} else {
					if (clickCall != null) {
						clickCall.call(BlendBtn.this);
					}
				}
				isTouched = false;
			}
		});
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		DU.drawTextureByActor(batch, parentAlpha, trNormal, this, overflows);
		if (isTouched) {
			DU.drawBlendOn(batch);
			DU.drawTextureByActor(batch, parentAlpha * blendAlpha, trNormal, this, overflows);
			DU.drawBlendOff(batch);
		}
	}

}
